<?php
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;

class Match extends Model
{
    protected $table = 'tmatch';

    protected  $fillable=['MTCustomerID', 'MTName','MTLeaderName','MTLeaderMobile','MTMatchID'];


    //报名or取消
    public static function doJoin($input = array())
    {

        $input['MTCustomerID'] = Session::get(User::$session_key);

        $input = array_only($input, array('MTCustomerID', 'MTName','MTLeaderName','MTLeaderMobile','MTMatchID'));

        $relative = DB::table('tmatchteam')->where(array_only($input,'MTMatchID','MTMatchID'))->first();

        if (!$relative)
        {
            return DB::table('tmatchteam')->insert($input);

        } else {
            return DB::table('tmatchteam')->where($input)->delete();

        }


    }

    public static function getList($input = array())
    {
        $entries=self::where('MEnrollEndAt', '>=', date('Y-m-d', time()))->orderBy('created_at', 'desc');


        if(isset($input['my']))
        {
            $customer_id=Session::get(User::$session_key);
            $arr=DB::table('tmatchteam')->where('MTCustomerID',$customer_id)->lists('MTMatchID');
            $arr=array_unique($arr);
            $entries=$entries->whereIn('MID',$arr);
        }

        $entries = $entries->paginate(5);

        return $entries;
    }

    public static  function getJoinList($input=array())
    {
        $input = array_only($input, array('MTMatchID','MTCustomerID'));
        return DB::table('tmatchteam')->where($input)->first();
    }


}